package service.user;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import service.CommonServlet;

public class AddFriend extends CommonServlet {
	
	@Override
	protected void execute(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		try
		{
			int fId = Integer.parseInt(request.getParameter("fId"));
			if(fId==0) 	throw new Exception("fId cann't be empty");
			
			int gNo = Integer.parseInt(request.getParameter("gNo"));
			if(gNo==0) 	throw new Exception("gNo cann't be empty");
			
			String sql0 = "select * from friend where u_noz = ? and u_noy = ? and g_no =?";
			PreparedStatement pstmt0 = mConnection.prepareStatement(sql0);
			pstmt0.setInt(1, fId);
			pstmt0.setInt(2, uNo);
			pstmt0.setInt(3, gNo);
			ResultSet rs = pstmt0.executeQuery();
			if(rs.next())
			{
				throw new Exception("friend exists already");
			}
			
			String sql = "insert into friend values (null, curdate(), ?, ?, ? )";
			PreparedStatement pstmt = mConnection.prepareStatement(sql);
			pstmt.setInt(1, fId);
			pstmt.setInt(2, uNo);
			pstmt.setInt(3, gNo);
			int result = pstmt.executeUpdate();
			
			response.getWriter().println(result);
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
	}

	/**
	 * Constructor of the object.
	 */
	public AddFriend() {
		super();
	}

}
